package com.ruoyi.ycl.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ycl.domain.vo.BsSupVo;
import com.ruoyi.ycl.domain.bo.BsSupBo;
import com.ruoyi.ycl.service.IBsSupService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供应商
 *
 * @author wdd
 * @date 2024-04-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ycl/bsSup")
public class BsSupController extends BaseController {

    private final IBsSupService iBsSupService;

    /**
     * 查询供应商列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<BsSupVo> list(BsSupBo bo, PageQuery pageQuery) {
        return iBsSupService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出供应商列表
     */
    @SaCheckLogin
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BsSupBo bo, HttpServletResponse response) {
        List<BsSupVo> list = iBsSupService.queryList(bo);
        ExcelUtil.exportExcel(list, "供应商", BsSupVo.class, response);
    }

    /**
     * 获取供应商详细信息
     *
     * @param supId 主键
     */
    @SaCheckLogin
    @GetMapping("/{supId}")
    public R<BsSupVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long supId) {
        return R.ok(iBsSupService.queryById(supId));
    }

    /**
     * 新增供应商
     */
    @SaCheckLogin
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BsSupBo bo) {
        return toAjax(iBsSupService.insertByBo(bo));
    }

    /**
     * 修改供应商
     */
    @SaCheckLogin
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BsSupBo bo) {
        return toAjax(iBsSupService.updateByBo(bo));
    }

    /**
     * 删除供应商
     *
     * @param supIds 主键串
     */
    @SaCheckLogin
    @Log(title = "供应商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{supIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] supIds) {
        return toAjax(iBsSupService.deleteWithValidByIds(Arrays.asList(supIds), true));
    }
}
